package com.wuliupai.delivery.wuliupaidelivery.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.App;
import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.PwdBean;
import com.wuliupai.delivery.wuliupaidelivery.constants.Value;
import com.wuliupai.delivery.wuliupaidelivery.net.NetResultOperate;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.LogUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.MD5;
import com.wuliupai.delivery.wuliupaidelivery.utils.SharePreferenceUtil;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

/**
 * 设置密码 或 修改密码界面
 */
@EActivity(R.layout.activity_set_password)
public class SetPasswordActivity extends AppBaseActivity {


    @Extra("ifModification")
    int ifModification; //(1)设置密码 (2)修改密码 (3)重置密码
    @Extra("ifSetupTradingPassword")
    boolean ifSetupTradingPassword;//是否设置交易密码 (true)设置交易密码 (false)设置账户密码


    @Bean(SharePreferenceUtil.class)
    SharePreferenceUtil mSharePreferenceUtil;
    @ViewById(R.id.et_my_wallet_password_old_pwd)
    EditText et_my_wallet_password_old_pwd;//输入原密码
    @ViewById(R.id.et_my_wallet_password_set_new_pwd)
    EditText et_my_wallet_password_set_new_pwd;//输入新密码
    @ViewById(R.id.et_my_wallet_password_confirm_pwd)
    EditText et_my_wallet_password_confirm_pwd;//输入确认密码
    @ViewById(R.id.ll_my_wallet_password_old_pwd)
    LinearLayout ll_my_wallet_password_old_pwd; //隐藏原密码
    @ViewById(R.id.tv_set_password_forgot_password)
    TextView tv_set_password_forgot_password; //忘记密码
    private String mAes;
    private String mPagerTitle;


    @Click(R.id.tv_set_password_forgot_password)
    void forgotPasswordClick() { //点击忘记密码
        RetrievePasswordActivity_.intent(this).start();
    }

    @Click(R.id.btn_password_set_submit)
    void SubmitPassWord() { //点击提交

        LogUtils.log("密码设置：ifModification= "+ifModification +" ifSetupTradingPassword= " +  ifSetupTradingPassword);

        PwdBean pwdbean = new PwdBean();
        String oldPwd = et_my_wallet_password_old_pwd.getText().toString();
        String newPwd = et_my_wallet_password_set_new_pwd.getText().toString();
        String newSurePwd = et_my_wallet_password_confirm_pwd.getText().toString();

        if (ifSetupTradingPassword) {   //交易密码
            if (ifModification == Value.SetPasswordType_Modification){
                //TODO 修改交易密码
                /*oldPayPassword
                  newPayPassword*/
                if (verifyPassword(oldPwd,newPwd,newSurePwd)){
                    String oldPwdmd5 = MD5.GetMD5Code(oldPwd);
                    String newPwdmd5 = MD5.GetMD5Code(newPwd);

                    pwdbean.setOldPayPassword(oldPwdmd5);
                    pwdbean.setNewPayPassword(newPwdmd5);

                    String PwdJson = JsonUtils.createJsonString(pwdbean);
                    LogUtils.log("上传修改交易密码" + PwdJson);
                    try {
                        mAes = JsonUtils.aes(PwdJson);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    callNet(this,"changepaypassword",mAes,operate);
                }
            }else if(ifModification == Value.SetPasswordType_Setting) {
                //TODO 设置交易密码
                //payPassword
                if (verifyPassword(oldPwd,newPwd,newSurePwd)){

                    String newPwdmd5 = MD5.GetMD5Code(newPwd);

                    pwdbean.setPayPassword(newPwdmd5);

                    String PwdJson = JsonUtils.createJsonString(pwdbean);
                    LogUtils.log("上传设置交易密码" + PwdJson);
                    try {
                        mAes = JsonUtils.aes(PwdJson);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    callNet(this,"submitpaypassword",mAes,operate);
                }
            }/*else if (ifModification == Value.SetPasswordType_Reset){
                //TODO 重置交易密码
                if (verifyPassword(oldPwd,newPwd,newSurePwd)){
                    pwdbean.setPayPassword(newPwd);
                }
            }*/
        } else {                        //账户密码
            if (ifModification == Value.SetPasswordType_Modification) {
                //TODO 修改账户密码
                /*oldPassword
                        newPassword*/
                alertPwd();
               /* if (verifyPassword(oldPwd,newPwd,newSurePwd)){
                    pwdbean.setOldPassword(oldPwd);
                    pwdbean.setNewPassword(newPwd);
                }*/
            } else if (ifModification ==  Value.SetPasswordType_Setting){
                //TODO 设置账户密码
            } /*else if (ifModification == Value.SetPasswordType_Reset){
                //TODO 重置账户密码
            }*/
        }
    }

    @AfterViews
    void onPageStart() {
        mPagerTitle = "";
        if (ifSetupTradingPassword) {   //交易密码
            if (ifModification == Value.SetPasswordType_Modification){
                //TODO 修改交易密码
                mPagerTitle = getResources().getString(R.string.string_my_modification_password_title);
                setDefaultTitle(this, R.string.string_my_modification_password_title_pay);
            }else if(ifModification == Value.SetPasswordType_Setting) {
                //TODO 设置交易密码
                mPagerTitle = getResources().getString(R.string.string_my_set_password_title);
                setDefaultTitle(this, R.string.string_my_set_password_title_pay);
                tv_set_password_forgot_password.setVisibility(View.GONE);
                ll_my_wallet_password_old_pwd.setVisibility(View.GONE);
            }/*else if (ifModification == Value.SetPasswordType_Reset){
                //TODO 重置交易密码
                setDefaultTitle(this, R.string.string_my_retrieve_password);
                ll_my_wallet_password_old_pwd.setVisibility(View.GONE);
            }*/
        } else {                        //账户密码
            if (ifModification == Value.SetPasswordType_Modification) {
                //TODO 修改账户密码
                mPagerTitle = getResources().getString(R.string.string_my_modification_password_title);
                setDefaultTitle(this, R.string.string_my_modification_password_title);
                tv_set_password_forgot_password.setVisibility(View.GONE);
            } else if (ifModification ==  Value.SetPasswordType_Setting){
                //TODO 设置账户密码
                mPagerTitle = getResources().getString(R.string.string_my_set_password_title);
                setDefaultTitle(this, R.string.string_my_set_password_title);
                tv_set_password_forgot_password.setVisibility(View.GONE);
                ll_my_wallet_password_old_pwd.setVisibility(View.GONE);
            } /*else if (ifModification == Value.SetPasswordType_Reset){
                //TODO 重置账户密码
                setDefaultTitle(this, R.string.string_my_retrieve_password);
                ll_my_wallet_password_old_pwd.setVisibility(View.GONE);
            }*/
        }


    }

    /**
     * 修改密码
     */
    private void alertPwd() {
        String oldPwd = et_my_wallet_password_old_pwd.getText().toString();
        String newPwd = et_my_wallet_password_set_new_pwd.getText().toString();
        String newSurePwd = et_my_wallet_password_confirm_pwd.getText().toString();
        if (newPwd.contains(newSurePwd)) {
            String oldPwdmd5 = MD5.GetMD5Code(oldPwd);
            String newPwdmd5 = MD5.GetMD5Code(newPwd);
            PwdBean pwdBean = new PwdBean(oldPwdmd5, newPwdmd5);
            String jsonAlertPwd = JsonUtils.createJsonString(pwdBean);
            LogUtils.log("修改密码send---" + jsonAlertPwd);
            try {
                String aesAlertPwd = JsonUtils.aes(jsonAlertPwd);
                callNet(SetPasswordActivity.this, "changepassword", aesAlertPwd, new NetResultOperate() {
                    @Override
                    public void onSuccess(String content) {
                        LogUtils.log("修改账户密码成功");
                        App.toast("修改账户密码成功");
                        finish();
                    }

                    @Override
                    public void onDataEmpty(String content) {

                    }

                    @Override
                    public void onDataEnd(String content) {

                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            App.toast(getString(R.string.string_pwd_unlike));
        }

    }

    /** 校验密码有效 */
    private boolean verifyPassword(String oldPwd,String newPwd,String newSurePwd){

        if (ifModification == Value.SetPasswordType_Modification){
            //TODO 修改交易密码
           if (!StringUtils.isEmpty(oldPwd)){
               if (!StringUtils.isEmpty(newPwd)){
                   if (newPwd.contains(newSurePwd)) {
                        return true;
                   }else{
                       App.toast(getString(R.string.string_pwd_unlike));
                   }
               }else {
                   App.toast("新密码为空");
               }
           }else {
               App.toast("原密码为空");
           }
        }else if(ifModification == Value.SetPasswordType_Setting) {
            //TODO 设置交易密码
            if (!StringUtils.isEmpty(newPwd)) {
                if (newPwd.contains(newSurePwd)) {
                    return true;
                } else {
                    App.toast(getString(R.string.string_pwd_unlike));
                }
            } else {
                App.toast("密码为空");
            }
        }else if (ifModification == Value.SetPasswordType_Reset){
            //TODO 重置交易密码
            if (!StringUtils.isEmpty(newPwd)) {
                if (newPwd.contains(newSurePwd)) {
                    return true;
                } else {
                    App.toast(getString(R.string.string_pwd_unlike));
                }
            } else {
                App.toast("密码为空");
            }
        }
        return false;
    }

    NetResultOperate operate = new NetResultOperate() {
        @Override
        public void onSuccess(String content) {
            LogUtils.log("密码"+content);
            mSharePreferenceUtil.setWhetherPayPassword(SetPasswordActivity.this,"1");
            App.toast(mPagerTitle+"成功");
            finish();
        }

        @Override
        public void onDataEmpty(String content) {
            LogUtils.log("密码onDataEmpty"+content);
        }

        @Override
        public void onDataEnd(String content) {
            LogUtils.log("密码onDataEnd"+content);
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
